package com.kevinwu.distributedsss.controller;

import com.kevinwu.distributedsss.pojo.Resume;
import com.kevinwu.distributedsss.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 简历业务处理控制器
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private IResumeService resumeService;

    /**
     * 查询所有记录
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView findAll(){
        List<Resume> list = resumeService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("index");
        return mv;
    }

    /**
     * 跳转到编辑页面
     * @param id
     * @return
     */
    @RequestMapping("/edit/{id}")
    public ModelAndView editRecord(@PathVariable("id") Long id){
        Resume resume = resumeService.finOne(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("resume",resume);
        mv.setViewName("edit");
        return  mv;
    }

    /**
     * 编辑信息后保存
     * @param resume
     * @return
     */
    @RequestMapping("/save")
    public ModelAndView savRecord(Resume  resume){
        resumeService.updateOne(resume);
        String url = "redirect:/resume/list";
        ModelAndView mav = new ModelAndView(url);
        return  mav;
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @RequestMapping("/insert")
    public ModelAndView insertRecord(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("insert");
        return  mv;
    }

    /**
     * 新增简历保存
     * @param resume
     * @return
     */
    @RequestMapping("/new")
    public ModelAndView insertRecord(Resume  resume){
        resumeService.insertOne(resume);
        String url = "redirect:/resume/list";
        ModelAndView mav = new ModelAndView(url);
        return  mav;
    }

    /**
     * 删除简历
     * @param id
     * @return
     */
    @RequestMapping("/del/{id}")
    public ModelAndView delRecord(@PathVariable("id") Long id){
        resumeService.deleteOne(id);
        String url = "redirect:/resume/list";
        ModelAndView mav = new ModelAndView(url);
        return  mav;
    }

}
